package com.cheyunkeji.er.adapter.evaluate;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.activity.evaluate.CarOwnerListActivity;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.evaluate.CustomerArchiveInfo;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class BindCarOwnerArchiveAdapter extends MyBaseAdapter {

    private static final String TAG = BindCarOwnerArchiveAdapter.class.getSimpleName();
    private int LAST_CHECKED_POSITION = -1;
    private onItemCBoxRecheckListener listener;

    public BindCarOwnerArchiveAdapter(ArrayList<CustomerArchiveInfo> dataList, Context context) {
        super(dataList, context);
        setOnItemCBoxRecheckListener((CarOwnerListActivity) context);
    }


    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.er_evaluate_add_client_listview_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position % 2 != 0)
            convertView.setBackgroundColor(Color.parseColor("#fbfbfb"));
        else
            convertView.setBackgroundColor(Color.parseColor("#ffffff"));

        CustomerArchiveInfo itemBean = (CustomerArchiveInfo) dataList.get(position);
        holder.tvOwnerName.setText(itemBean.getRealname());
        holder.tvTeleNumber.setText(itemBean.getPhone());
        holder.tvAddress.setText(itemBean.getAddress());

        holder.ivGender.setImageResource(itemBean.getSex().equals("1") ? R.mipmap.er_icon_male_white : R.mipmap.er_icon_female_white);
        holder.ivGender.setBackgroundResource(itemBean.getSex().equals("1") ? R.drawable.er_bkg_dark_blue_rect : R.drawable.er_bkg_pink_rect);
        holder.ivPersonalOrCompany.setImageResource(itemBean.getCategory().equals("1") ? R.mipmap.er_icon_user_personal : itemBean.getCategory().equals("2") ? R.mipmap.er_icon_company : R.mipmap.er_icon_xzdw_white);


        holder.cbAdd.setTag(position);

        holder.cbAdd.setOnCheckedChangeListener(

                new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                        if (isChecked) {
                            LAST_CHECKED_POSITION = position;
                            notifyDataSetChanged();
//                            Log.e(TAG, "onCheckedChanged:  Position : " + position);
                            listener.onItemChecked(LAST_CHECKED_POSITION);

                        } else {
                            if (position == LAST_CHECKED_POSITION) {
                                LAST_CHECKED_POSITION = -1;
                                notifyDataSetChanged();
                                listener.onItemChecked(LAST_CHECKED_POSITION);

                            }
                        }

                        listener.onItemChecked(LAST_CHECKED_POSITION);
                        Log.e(TAG, "onCheckedChanged: Current Item Position : " + position );
                        Log.e(TAG, "CheckedChanged: Checked Position :  " + LAST_CHECKED_POSITION  );

                    }
                });

        if (position == LAST_CHECKED_POSITION) {
            holder.cbAdd.setChecked(true);
        } else {
            holder.cbAdd.setChecked(false);
        }

        return convertView;

    }


//    更新当前选中的item下标
    public void setLAST_CHECKED_POSITION(int position){
        if(this.LAST_CHECKED_POSITION == position){
//          反选
            LAST_CHECKED_POSITION = -1 ;
        }else
        this.LAST_CHECKED_POSITION = position ;
    }


    public void setOnItemCBoxRecheckListener(onItemCBoxRecheckListener listener) {

        this.listener = listener;
    }


    public interface onItemCBoxRecheckListener {

        void onItemChecked(int position);

    }

    static class ViewHolder {
        @BindView(R.id.cb_add)
        CheckBox cbAdd;
        @BindView(R.id.tv_owner_name)
        TextView tvOwnerName;
        @BindView(R.id.iv_personal_or_company)
        ImageView ivPersonalOrCompany;
        @BindView(R.id.iv_gender)
        ImageView ivGender;
        @BindView(R.id.rl_img_display)
        RelativeLayout rlImgDisplay;
        @BindView(R.id.tv_tele_number)
        TextView tvTeleNumber;
        @BindView(R.id.tv_address)
        TextView tvAddress;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
